/**
 * Huize Service Inc
 * All Rights Reserved @2018
 */

package com.huize.zhike.framework.common.enumeration;

import com.huize.ladon.apm.common.enums.EnumInterface;
import org.apache.commons.lang3.StringUtils;

import java.util.stream.Stream;

/**
 * 描述: 数据状态枚举
 *
 * @author tianyuliang
 * @version $Id: DeletionEnum.java, v0.1 2020/3/19 19:41 tianyuliang Exp $$
 */
public enum DeletionEnum implements EnumInterface {

    /**
     * 正常
     */
    OK("ok", true, 0, "正常"),

    /**
     * 已删除
     */
    DELETED("deleted", false, 1, "已删除"),

    ;

    private String code;

    private Boolean flag;

    private Integer value;

    private String desc;

    DeletionEnum(String code, Boolean flag, Integer value, String desc) {
        this.code = code;
        this.flag = flag;
        this.value = value;
        this.desc = desc;
    }

    public static DeletionEnum of(String code) {
        if (StringUtils.isBlank(code)) {
            return DELETED;
        }

        return Stream.of(values()).filter(b -> b.getCode().equalsIgnoreCase(code.trim())).findFirst().orElseGet(() -> DELETED);
    }

    /**
     * 校验是否已被删除 true:已被删除, false:无效
     *
     * @param code
     * @return
     */
    public static boolean isDeleted(String code) {
        return of(code) == DELETED;
    }

    /**
     * 校验是否正常 true:正常有效, false:被删除
     *
     * @param code
     * @return
     */
    public static boolean isOk(String code) {
        return of(code) == OK;
    }



    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDesc() {
        return desc;
    }

    public Boolean getFlag() {
        return flag;
    }

    public Integer getValue() {
        return value;
    }

    @Override
    public String toString() {
        return String.format("[%s,%s,%s]", getCode(), getValue(), getDesc());
    }

}
